package com.dp.util;


/**
 * <p>Title: 锟斤拷页锟斤拷示锟斤拷锟?/p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author
 * @version 1.0
 */
import java.util.ArrayList;
import java.util.HashMap;


import com.dp.pub.MyServer;
import com.dp.pub.SysProperty;
import com.dp.service.impl3.BaseImpl;


public class PageBean {
    private int recCount; //记录总数
    private int curPage; //当前页号
//    private int maxRows = 1; //每页记录数 test
    private int maxRows = Integer.parseInt(SysProperty.getPrep("rowperpage")); //每页记录数
    private int fromRow; //当前页的开始记录
    private int toRow; //当前页的结束记录
    private int pageCount; //总页数
    private int id; //总页数
    private ArrayList NewData; // 当前页面所有数据
 //   private DBOperate db = null; //数据库连接bena
    private String Message = null;
    private String MinuteMessage = null;
    private DataMap hashMap = null;

    //private String sql;    //当前查询的SQL
    public PageBean() {
    }

    public PageBean(DataMap hashMap) //throws Exception
     {
        this.hashMap = hashMap;
        this.curPage = this.parseInt(this.ObjectToString(hashMap.get("curPage")));
        this.pageCount = this.parseInt(this.ObjectToString(hashMap.get(
                        "pageCount")));
//        this.maxRows = this.parseInt(this.ObjectToString(hashMap.get("maxRows")));
//
//        if (this.maxRows == 1) {
//            this.maxRows = 50;
//        }
//        this.maxRows = 5;
        this.setCurPage(this.judgeButton(hashMap));

//        db = new DBOperate();
        BaseImpl t=(BaseImpl) MyServer.ctx.getBean("baseServiceImpl3");
        try {
            Object[] obj = t.queryByResultSet(this.ObjectToString(hashMap.getMapData(
                            "sql")), this.getFromRow(), this.getMaxRows());

           
            //取记录数
            setRecCount(Integer.parseInt(obj[0].toString()));

            //总页数
            setPageCount(Integer.parseInt(obj[1].toString()));

            //存储当前数据
            setPageData((ArrayList) obj[2]);

            setFromRow(this.fromRow);

            //当前页的结束记录
            setToRow(this.fromRow * this.maxRows);
        } catch (Exception e) {
            Message = InterUtil.rb.getString("src.dbConErr");
            MinuteMessage = e.getMessage();
            setPageData(null);
        }
    }

    //取记录数
    public DataMap getMap() {
        return this.hashMap;
    }

    public int getID() {
        if (curPage > 1) {
            return (curPage * maxRows) - (maxRows - 1);
        } else {
            return 1;
        }
    }

    public int getRecCount() {
        return recCount;
    }

    public String getMessage() {
        return Message;
    }

    public String getMinuteMessage() {
        return MinuteMessage;
    }

    public void setRecCount(int recCount) {
        this.recCount = recCount;
    }

    //取当前页号
    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    //每页行数
    public int getMaxRows() {
        return maxRows;
    }

    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    //当前页的开始记录
    public int getFromRow() {
        if (this.curPage > 1) {
            fromRow = (this.curPage - 1) * this.maxRows;
        } else {
            fromRow = 1;
        }

        if (this.curPage != 1) {
            fromRow++;
        }

        return fromRow;
    }

    public void setFromRow(int fromRow) {
        this.fromRow = fromRow;
    }

    //当前页的结束记录
    public int getToRow() {
        toRow = this.getFromRow();

        if ((toRow + this.maxRows) < this.recCount) {
            toRow += (this.maxRows - 1);
        } else {
            toRow = this.recCount;
        }

        return toRow;
    }

    public void setToRow(int toRow) {
        this.toRow = toRow;
    }

    //取页数
    public int getPageCount() {
        pageCount = this.recCount / this.maxRows;

        int m = this.recCount % this.maxRows;

        if (m > 0) {
            pageCount += 1;
        }

        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    //取得当前页面数据
    public ArrayList getPageData() {
        return this.NewData;
    }

    private void setPageData(ArrayList data) {
        this.NewData = data;
    }

    //当前查询的SQL

    /*public String getSql(){
            return this.sql;
            }

    public void setSql(String strsql){
         this.sql = strsql;
       }*/

    //取显示页信息
    public String getPageString() {
//        if (this.recCount < this.maxRows) {
//            return "";
//        }

       /* String st = InterUtil.rb.getString("src.thepagenumber") +" "+ this.curPage + " "+InterUtil.rb.getString("src.pagepageandpage")+" " + this.getPageCount() +
            " "+InterUtil.rb.getString("src.pages");
*/
    	String st = this.curPage + " / " + this.getPageCount()+"&nbsp;&nbsp;";
        return st;
    }

    //取显示记录信息
    public String getRecString() {
//        if (this.recCount < this.maxRows) {
//            return "";
//        }

        String st = 
//        	" " + this.getFromRow() + " 至 " + this.getToRow() + " / " + 
            " "+InterUtil.rb.getString("src.recordtotal")+" " + this.recCount + " "+InterUtil.rb.getString("src.records")+" ";

        return st;
    }

    //取页导航信息
    public String getNavPage() {
//        if (this.recCount < this.maxRows) {
//            return "";
//        }

        String st = "";

        if (this.getPageCount() > 0) {
            if (this.getCurPage() != 1) {
                st += (this.getPageButton(InterUtil.rb_jsp.getString("label.first.page")) + "  " +
                this.getPageButton(InterUtil.rb_jsp.getString("label.previous.page")));
            }

            if (this.getCurPage() != this.getPageCount()) {
                st += ("  " + this.getPageButton(InterUtil.rb_jsp.getString("label.next.page")) + "  " +
                this.getPageButton(InterUtil.rb_jsp.getString("label.last.page")));
            }
        }

        return st;
    }

    //生成按钮
    public String getPageButton(String SubmitValue) {
//        if (this.recCount < this.maxRows) {
//            return "";
//        }

        String st = "";
        st = "<input type=\"hidden\" name=\"RequestType\" value=''>" +
            "<input type=\"submit\" value=\"" + SubmitValue +
            "\" name=\"Submit\" class=\"btf\">";

        return st;
    }

    //跳转至指定页面
    public String getGoPage() {
//        if (this.recCount < this.maxRows) {
//            return "";
//        }

        if (this.curPage == 0) {
            this.curPage = 1;
        }

        String st = "GOTO";
        st = "<input type=\"text\" name=\"PageGoToCount\" WIDTH=10 size=2 value='" +
            this.curPage + "' >" + "   " +
            "<input type=\"hidden\" name=\"RequestType\" value=\"search\">" +
            "<input type=\"submit\" value=\"Go\" name=\"Submit\" class=\"btf\">";

        return st;
    }

    public int judgeButton(HashMap hashMap) {
        int result = 1;
        String submit = this.ObjectToString(hashMap.get("Submit"));

        if (submit != null) {
            if (submit.equals(InterUtil.rb_jsp.getString("label.first.page"))) {
                result = 1;
            } else if (submit.equals(InterUtil.rb_jsp.getString("label.previous.page"))) {
                result = this.curPage - 1;
            } else if (submit.equals(InterUtil.rb_jsp.getString("label.next.page"))) {
                if (this.curPage >= this.pageCount) {
                    result = this.pageCount;
                } else {
                    result = this.curPage + 1;
                }
            } else if (submit.equals(InterUtil.rb_jsp.getString("label.last.page"))) {
                result = this.pageCount;
            } else if (submit.equals("Go")) {
                String PageGoToCount = this.ObjectToString(hashMap.get(
                            "PageGoToCount"));
                int GoToCount = this.parseInt(PageGoToCount);

                if (GoToCount <= this.curPage) {
                    result = GoToCount;
                } else {
                    if (this.curPage >= this.pageCount) {
                        result = this.pageCount;
                    } else {
                        result = GoToCount;
                    }
                }
            }
        }

        return result;
    }

    public String ObjectToString(Object obj) {
        if ((obj != null) && !obj.equals("") && !obj.equals("null")) {
            return obj.toString();
        } else {
            return "";
        }
    }

    private int parseInt(String PageGoToCount) {
        try {
            return Integer.parseInt(PageGoToCount);
        } catch (Exception e) {
            return 1;
        }
    }

    public static void main(String[] a) {
        new PageBean();
    }
}
